package cn.highedu.coolsharkhub.service.impl;

import cn.highedu.coolsharkhub.entity.Category;
import cn.highedu.coolsharkhub.entity.vo.ProductHomepageVO;
import cn.highedu.coolsharkhub.mapper.CategoryMapper;
import cn.highedu.coolsharkhub.mapper.ProductMapper;
import cn.highedu.coolsharkhub.service.ProductService;
import cn.highedu.coolsharkhub.service.exception.BlankParameterException;
import cn.highedu.coolsharkhub.service.exception.EntityNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品业务层实现类
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    CategoryMapper categoryMapper;
    /**
     * 根据商品类别ID查询相应商品
     * @param categoryId 类别id
     * @return 商品列表
     */
    @Override
    public List<ProductHomepageVO> findAllHomepageProduct(Integer categoryId) {
        if (categoryId == null) {
            throw new BlankParameterException("类别id不能为空");
        }
        // 查询商品类别
        Category category = categoryMapper.selectById(categoryId);
        if (category == null) {
            throw new EntityNotFoundException("类别不存在");
        }
        return productMapper.selectAllHomepageProduct(categoryId);
    }
}
